package hz2py

import (
	"strings"

	cnt "fyne.io/fyne/v2/container"
	wdg "fyne.io/fyne/v2/widget"

	"fyne.io/fyne/v2"
	"github.com/Chain-Zhang/pinyin"
)

func UI(a fyne.App) {
	w := a.NewWindow("Hz2py")
	w.Resize(fyne.NewSize(600, 600))
	txtResult := wdg.NewMultiLineEntry()
	txtHanzi := wdg.NewMultiLineEntry()
	txtHanzi.SetText(`
			   静夜思
		【作者】李白 【朝代】唐
		床前明月光，疑是地上霜。
		举头望明月，低头思故乡。`)
	split := cnt.NewHSplit(txtHanzi, txtResult)
	// split.Offset = 0.2
	btn := wdg.NewButton("中文汉字转拼音", func() {
		txtResult.SetText(hanzi2pin(txtHanzi.Text))
	})
	lblMsg := wdg.NewLabel("")
	cc := cnt.NewBorder(btn, lblMsg, nil, nil, split)
	w.SetContent(cc)
	w.CenterOnScreen()
	w.Show()
}

func hanzi2pin(hs string) string {
	var elems []string = []string{}
	// 	InitialsInCapitals: 首字母大写, 不带音调
	// WithoutTone: 全小写,不带音调
	// Tone: 全小写带音调
	str1, err := pinyin.New(hs).Split("").Mode(pinyin.InitialsInCapitals).Convert()
	if err != nil {
		// 错误处理
	}

	str2, err := pinyin.New(hs).Split(" ").Mode(pinyin.WithoutTone).Convert()
	if err != nil {
		// 错误处理
	}

	str3, err := pinyin.New(hs).Split("-").Mode(pinyin.Tone).Convert()
	if err != nil {
		// 错误处理
	}

	str4, err := pinyin.New(hs).Convert()
	if err != nil {
		// 错误处理
	}
	elems = append(elems, str1, str2, str3, str4)

	return strings.Join(elems, "\n\n")
}
